﻿package {
	
import flash.events.Event;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.net.Responder;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.text.TextField;


	
public class Flupal extends NetConnection {

	// Constructor
	// -----------------------------------------------------------
	
	public function Flupal() {
		
		super();
		
		this.addEventListener	(NetStatusEvent.NET_STATUS, netStatusHandler);
		this.objectEncoding 		= flash.net.ObjectEncoding.AMF0;
		this.connect			("/services/amfphp/");
	}
	
	
	
	// Public Functions
	// -----------------------------------------------------------
	
	public function request($method:String, $handler:Function, ...args):void {
		
		switch (args.length) {
			
			case 0:
			this.call($method, new Responder($handler, errorHandler));
			break;
			
			case 1:
			this.call($method, new Responder($handler, errorHandler), args[0]);
			break;
			
			case 2:
			this.call($method, new Responder($handler, errorHandler), args[0], args[1]);
			break;
			
			case 3:
			this.call($method, new Responder($handler, errorHandler), args[0], args[1], args[2]);
			break;
			
			case 4:
			this.call($method, new Responder($handler, errorHandler), args[0], args[1], args[2], args[3]);
			break;
			
			case 5:
			this.call($method, new Responder($handler, errorHandler), args[0], args[1], args[2], args[3], args[4]);
			break;
			
			default:
			this.call($method, new Responder($handler, errorHandler));
			break;
		}
	}
	
	
	
	// Event Handlers
	// -----------------------------------------------------------
	
	private function netStatusHandler(nse:NetStatusEvent):void {
		
		trace(nse.info.code);
	}
	
	private function errorHandler(error:Object):void {
		
		trace(error.toString());
	}
}

}